////////////////////////////////////////////////////////////////////////////////
//////// PRESIDENTIAL ELECTIONS - TREATMENT ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

// This do-file associates each presidential election with a running variable and treatment variable. 

////////////////////////////////////////////////////////////////////////////////
//////// a. Selecting presidential elections of interest
////////////////////////////////////////////////////////////////////////////////

// We import the presidential election database
use "$project_path/data/3_cleaned/presidential_elections", clear

// We find the year of the last and next election
sort Country Year Month
gen last_election = .
gen next_election = .
replace last_election = Year[_n-1] if Country[_n-1]==Country[_n]
replace next_election = Year[_n+1] if Country[_n+1]==Country[_n]

// We flag consequential elections
gen consequential = 1 if flag_not_last==. & flag_inconsequential==.

// We find the year of the last/next consequential election by looping over observations
sort Country Year Month
gen last_consequential = .
gen next_consequential = .
// Looping over observations
local N = _N
forvalues i = 1/`N' {
	// For each election, we go four elections back to get the previous consequential election
	forvalues j=1/4 {
		if Country[`i'-`j']==Country[`i'] & consequential[`i'-`j']==1 & last_consequential[`i']==. {
			qui replace last_consequential = Year[`i'-`j'] in `i'
		}
		if Country[`i'+`j']==Country[`i'] & consequential[`i'+`j']==1 & next_consequential[`i']==. {
			qui replace next_consequential = Year[`i'+`j'] in `i'
		}
	}
}

// We ignore elections for which we have no results
drop if Source==""

// We ignore elections where there was a single candidate
drop if flag_plebiscite==1 | flag_unopposed==1

////////////////////////////////////////////////////////////////////////////////
//////// b. Find the role of each election (who is the elected leader)
////////////////////////////////////////////////////////////////////////////////

// Adding data on the leader elected during each election.
merge 1:1 Country Year Month Type_Election using "$project_path/data/2_intermediary/leaders/elected_leaders", assert(using match) keep(match) nogen
assert (elected_leader=="HOG") | (elected_leader=="HOS")

// The elected_leader variable is mostly set to HOS. For the few elections associated with HOG, we made manual checks
tab elected_leader
list Country Year if elected_leader=="HOG"

sort Country Year Month

tempfile election_results
save `election_results'

// We build a list of candidates and parties
use `election_results', clear
forvalues i=1/50 {
	ren Party_`i' party_`i'
	ren Candidate_`i' candidate_`i'
	order candidate_`i' party_`i', last
}
keep Country Year Month elected_leader candidate* party*
tempfile candidates
save `candidates'

// We build a list of election scores
use `election_results', clear
forvalues i = 2/50 {
	assert E_Vote_Share1_`i' == . if E_Vote_Share1_1==.
	assert E_Vote_Share2_`i' == . if E_Vote_Share2_1==.
	assert E_Vote_Share3_`i' == . if E_Vote_Share3_1==.
}
forvalues i=1/50 {
	ren Party_`i' party_`i'
	ren Candidate_`i' candidate_`i'
	qui gen double party_share_`i' = Vote_Share1_`i'
	qui replace party_share_`i' = Vote_Share2_`i' if flag_two_round==1
	qui replace party_share_`i' = E_Vote_Share1_`i' if flag_indirect==1 & E_Vote_Share1_1!=.
	qui replace party_share_`i' = E_Vote_Share2_`i' if flag_indirect==1 & E_Vote_Share2_1!=.
	qui replace party_share_`i' = E_Vote_Share3_`i' if flag_indirect==1 & E_Vote_Share3_1!=.
	order candidate_`i' party_`i' party_share_`i', last
}
keep Country Year Month elected_leader candidate* party* last_election next_election consequential last_consequential next_consequential indirect_to_include
tempfile scores
save `scores'

// We build a list of flags
use `election_results', clear
keep Country Year Month Type_Election flag*
tempfile flags
save `flags'

////////////////////////////////////////////////////////////////////////////////
//////// c. Defining an incumbent leader/party
////////////////////////////////////////////////////////////////////////////////

**	(1)	Automatic assignment

// We retrieve the output of the preliminary task associating elections to incumbents
import excel "$project_path/data/2_intermediary/leaders/leaders_before_after.xlsx", firstrow clear
destring Month, replace

// We merge with the election results
merge 1:1 Country Year Month Type_Election using `election_results', assert(master match) keep(match) nogen

// We assign incumbent leader and incumbent party depending on the role of the election
gen incumbent_leader = ""
gen incumbent_party = ""
gen leader_after = ""
gen party_after = ""
foreach leader_type in HOS HOG {
	replace incumbent_leader = incumbent_leader_`leader_type' if elected_leader=="`leader_type'"
	replace incumbent_party = incumbent_party_`leader_type' if elected_leader=="`leader_type'"
	replace leader_after = leader_after_`leader_type' if elected_leader=="`leader_type'"
	replace party_after = party_after_`leader_type' if elected_leader=="`leader_type'"
}
drop incumbent_leader_HOS incumbent_party_HOS incumbent_leader_HOG incumbent_party_HOG leader_after_HOS leader_after_HOG party_after_HOS party_after_HOG

**	(2)	Manual assignment

// When the automatic method fails to associate an election with an incumbent leader or party, we try to define one manually
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/incumbents.xlsx", firstrow clear
ren incumbent_leader manual_incumbent_leader
ren incumbent_party manual_incumbent_party
merge 1:1 Country Year Month elected_leader using `temp', assert(using match)
replace incumbent_leader = manual_incumbent_leader if manual_incumbent_leader!=""
replace incumbent_party = manual_incumbent_party if manual_incumbent_party!=""

// We check that cases with no incumbent leader *and* no incumbent party have been checked manually
assert _merge==3 if incumbent_leader=="" & incumbent_party==""

// We keep the relevant variables
order Country Year Month, first
keep Country Year Month incumbent_leader incumbent_party leader_after party_after elected_leader

replace incumbent_party="" if incumbent_party=="independent politician" | incumbent_party=="Independent" | incumbent_party=="nonpartisanism" | incumbent_party=="independent politician // nonpartisanism"

tempfile incumbents
save `incumbents'

////////////////////////////////////////////////////////////////////////////////
//////// d. Defining a candidate of the incumbency
////////////////////////////////////////////////////////////////////////////////

// We performed an fuzzy match using the fuzzywuzzy package in Python to identify candidates of the incumbency. If the incumbent leader is a candidate in the election, they are designed as the candidate of the incumbency. Else, if the incumbent party is present in the candidate  parties, the candidate running for this party is designed as the candidate of  the incumbency. Fuzzy matches were systematically manually checked.

// Recovering the output of the fuzzy match
import excel "$project_path/data/1_input/manual_matches/presidential/representative_incumbency_fuzzy_match.xlsx", firstrow clear

// Manual adjustments to the fuzzy match
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/representative_incumbency_manual_match.xlsx", firstrow clear
ren candidate_matched manual_candidate_matched
ren party_matched manual_party_matched
merge 1:1 Country Year Month incumbent_leader incumbent_party using `temp', assert(using match) nogen
replace candidate_matched = manual_candidate_matched if manual_candidate_matched!=""
replace party_matched = manual_party_matched if manual_party_matched!=""
drop manual_candidate_matched manual_party_matched

// Adding manually defined candidates of the incumbency
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/representative_incumbency_manual.xlsx", firstrow clear
keep Country Year Month candidate_incumbency
ren candidate_incumbency manual_candidate_incumbency
merge 1:1 Country Year Month using `temp', assert(using match) nogen
replace candidate_matched = manual_candidate_incumbency if manual_candidate_incumbency!=""
drop manual_candidate_incumbency

// Merging with the scores database, in order to find the score of the incumbent candidate/party, as well as the leaders/parties in power before/after the election
merge 1:1 Country Year Month using `scores', assert(match) nogen
merge 1:1 Country Year Month using `incumbents', assert(match) nogen

// Rank of candidate/party of the incumbency
gen rank_incumbent_leader = .
gen rank_incumbent_party = .
gen candidate_incumbent_party = ""
forvalues i=1/50 {
	replace rank_incumbent_leader = `i' if candidate_matched==candidate_`i' & candidate_matched!=""
	replace rank_incumbent_party = `i' if party_matched==party_`i' & party_matched!=""
	replace candidate_incumbent_party = candidate_`i' if party_matched==party_`i' & party_matched!=""
}

// We check that the matched parties and candidates that have been found are all associated with a rank (this is to check that manually added matches are correct, that there is no spelling mistake for example)
assert !(party_matched!="" & rank_incumbent_party==.)
assert !(candidate_matched!="" & rank_incumbent_leader==.)

// We define the candidate of the incumbency 
gen candidate_incumbency = candidate_matched
replace candidate_incumbency = candidate_incumbent_party if candidate_incumbency==""
gen rank_incumbency = rank_incumbent_leader
replace rank_incumbency = rank_incumbent_party if rank_incumbency==.

*We define the opposition as the best ranked candidate (apart from the candidate representing the incumbency)
gen rank_opposition = 1 if rank_incumbency>1 & rank_incumbency!=.
replace rank_opposition = 2 if rank_incumbency==1

////////////////////////////////////////////////////////////////////////////////
//////// e. Defining running and treatment variables
////////////////////////////////////////////////////////////////////////////////

// Define the running variable
gen double share_incumbent = .
gen double share_opposition = .
gen party_candidate_incumbency = ""
gen party_candidate_opposition = ""
gen candidate_opposition = ""
forvalues i=1/50 {
	replace share_incumbent = party_share_`i' if rank_incumbency==`i'
	replace share_opposition = party_share_`i' if rank_opposition==`i'
	replace party_candidate_incumbency = party_`i' if rank_incumbency==`i'
	replace party_candidate_opposition = party_`i' if rank_opposition==`i'
	replace candidate_opposition = candidate_`i' if rank_opposition==`i'
}

gen double runvar = share_opposition - share_incumbent if share_incumbent!=. & share_opposition!=.

// Define the treatment variable -- the treatment is defined with the rank of the incumbency rather than the running variable alone (as for parliamentary elections) because there are cases where the treatment is defined but not the running variable (e.g., the 2017 French election, because the candidate of the incumbency didn't make it to the second round).
gen treatment = 0 if rank_incumbency==1
replace treatment = 1 if rank_incumbency>1 & rank_incumbency!=.

// Check sign if the treatment variable
assert !(treatment==0 & runvar>0 & runvar!=.)
assert !(treatment==1 & runvar<0 & runvar!=.)

// Special case -- Argentina 2003 (Carlos Menem dropped out of the second round, facing N. Kirchner -- who was the candidate of the incumbency)
replace treatment = . if Country=="Argentina" & Year==2003

// We check that all elections have been correctly treated
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/no_runvar.xlsx", firstrow clear
keep Country Year Month
gen no_runvar = 1
merge 1:1 Country Year Month using `temp', assert(using match) nogen
assert !(rank_incumbency==. & no_runvar!=1)

replace runvar = . if no_runvar==1

// We build a variable corresponding to the value of the treatment for the last/next consequential election.
sort Country Year Month
gen last_treatment=.
gen next_treatment=.
gen last_runvar=.
gen next_runvar=.

local N = _N
//Looping over observations
forvalues i = 1/`N' {
	//For each election, we go four elections back/forward to get the previous/next consequential election
	forvalues j=1/4 {
		if Country[`i'-`j']==Country[`i'] & last_consequential[`i']==Year[`i'-`j'] & consequential[`i'-`j']==1  {
			qui replace last_treatment = treatment[`i'-`j'] in `i'
			qui replace last_runvar = runvar[`i'-`j'] in `i'
		}
		if Country[`i'+`j']==Country[`i'] & next_consequential[`i']==Year[`i'+`j'] & consequential[`i'+`j']==1  {
			qui replace next_treatment = treatment[`i'+`j'] in `i'
			qui replace next_runvar = runvar[`i'+`j'] in `i'
		}
	}
}

// Time elapsed since last treatment 
gen elapsed_last_treatment = .
sort Country Year Month 
forvalues k = 1/30 {
	qui replace elapsed_last_treatment = Year - Year[_n-`k'] if elapsed_last_treatment==. & Country==Country[_n-`k'] & treatment[_n-`k']==1
}

////////////////////////////////////////////////////////////////////////////////
//////// f. Turnover in the executive branch
////////////////////////////////////////////////////////////////////////////////

// For elections with a running variable, we assign an alternative treatment variable (for turnover in the executive branch). We determine whether there was an turnover in the executive branch by comparing the leader/party in power before/after the election
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/executive_turnovers.xlsx", firstrow clear
destring treatment_alt, replace
merge 1:1 Country Year Month incumbent_leader incumbent_party leader_after party_after using `temp', assert(using match) nogen

// We manually check cases where treatment and treatment_alt do no match
gen to_check = 0
replace to_check = 1 if treatment!=treatment_alt & runvar!=.
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/presidential/executive_turnovers_checks.xlsx", firstrow clear
keep Country Year Month treatment_alt
ren treatment_alt treatment_alt_manual
gen checked = 1
merge 1:1 Country Year Month using `temp', assert(using match) nogen

// We check that all the entries that needed to be checked have indeed been checked
count if to_check==1 & checked!=1
assert `r(N)'==0

// We update manually coded variables
replace treatment_alt = treatment_alt_manual if checked==1
drop treatment_alt_manual to_check checked

// The running variable for the alternative specification is the same as in the baseline specification
gen runvar_alt = runvar if Year>1945

////////////////////////////////////////////////////////////////////////////////
//////// j. Wikidata ID of the leader before the election and the candidate of the incumbency
////////////////////////////////////////////////////////////////////////////////

replace incumbent_leader = strtrim(stritrim(incumbent_leader))
replace candidate_incumbency = strtrim(stritrim(candidate_incumbency))

***	Leaders before the election
tempfile temp
save `temp'
import excel "$project_path/data/1_input/other/leaders/party_affiliations.xlsx", firstrow clear
replace Leader = strtrim(stritrim(Leader))
keep Country Leader wikidata_id
ren Leader incumbent_leader
ren wikidata_id incumbent_leader_wid
format incumbent_leader %20s
merge 1:m Country incumbent_leader using `temp', keep(using match)
assert incumbent_leader=="" if _merge==2
drop _merge

***	Candidates of the incumbency
tempfile temp
save `temp'
import excel "$project_path/data/1_input/other/leaders/party_affiliations.xlsx", firstrow clear
replace Leader = strtrim(stritrim(Leader))
keep Country Leader wikidata_id
ren Leader candidate_incumbency
ren wikidata_id candidate_incumbency_wid
merge 1:m Country candidate_incumbency using `temp', keep(using match)
assert candidate_incumbency=="" if _merge==2
drop _merge

////////////////////////////////////////////////////////////////////////////////
///////// Exporting results
////////////////////////////////////////////////////////////////////////////////

// Adding flags 
merge 1:1 Country Year Month using `flags', assert(match) nogen

local vars_to_keep = "Country Year Month Type_Election runvar runvar_alt treatment treatment_alt incumbent_leader incumbent_party incumbent_leader_wid leader_after party_after rank_incumbency candidate_incumbency party_candidate_incumbency candidate_incumbency_wid rank_opposition candidate_opposition party_candidate_opposition share_incumbent share_opposition flag* last_election next_election last_consequential next_consequential last_treatment last_runvar next_treatment next_runvar elapsed_last_treatment"
keep `vars_to_keep'
order `vars_to_keep'

lab var Month				 			"Month"
lab var Type_Election					"Election type"
lab var incumbent_leader				"Incumbent leader"
lab var incumbent_party					"Incumbent party"
lab var leader_after				 	"Leader after the election"
lab var party_after				 		"Party after the election"
lab var rank_incumbency				 	"Rank of the incumbency"
lab var candidate_incumbency			"Candidate representing the incumbency"
lab var party_candidate_incumbency		"Party of the candidate representing the incumbency"
lab var rank_opposition					"Rank of the opposition"
lab var candidate_opposition			"Candidate representing the opposition"
lab var party_candidate_opposition		"Party of the candidate representing the opposition"
lab var candidate_incumbency_wid 		"Candidate representing the incumbency Wikidata ID"
lab var incumbent_leader_wid			"Incumbent leader Wikidata ID"
lab var share_incumbent				 	"Vote share of the candidate representing the incumbency"
lab var share_opposition				"Vote share of the candidate representing the opposition"
lab var runvar							"Running variable (electoral turnover)"
lab var runvar_alt						"Running variable (executive turnover)"
lab var treatment						"Electoral turnover"
lab var treatment_alt					"Executive turnover"
lab var flag_inconsequential			"Flags inconsequential elections"
lab var last_election				 	"Year of the previous election"
lab var next_election					"Year of the next election"
lab var last_consequential				"Year of the last consequential election"
lab var next_consequential				"Year of the next consequential election"
lab var last_treatment					"Electoral turnover at the previous consequential election"
lab var next_treatment					"Electoral turnover at the next consequential election"
lab var last_runvar						"Running variable at the previous consequential election"
lab var next_runvar						"Running variable at the previous consequential election"
lab var elapsed_last_treatment 			"Time elaspsed since last treatment"

compress

format  Country incumbent_leader incumbent_party leader_after party_after candidate_incumbency party_candidate_incumbency candidate_opposition party_candidate_opposition flag_inconsequential_note %20s
format candidate_incumbency_wid incumbent_leader_wid %15s
sort Country Year Month

save "$project_path/data/3_cleaned/turnovers_presidential", replace
